<?
$nntp = new NNTP($nntp_server, $user, $pass);    
if (!$nntp->connect()) 
{ 
	header("Content-Error: " . $nntp->get_error_message());
	echo "<b>".$messages_ini["error"]["nntp_fail"]."</b><br>"; 
	echo $nntp->get_error_message()."<br>"; 
} 
else
{
	if ($group_name!=""&&$gi=$nntp->join_group($group_name))
	{    
		 
		 
		 #--------------------------------------------------------------------------------------
		 if (strlen($search_param) > 0)
		 {  // search the group for specific articles      
			# $result=$nntp->search_newsgroup(1, "*" . $search_param . "*", "from");
			$result=$nntp->search_newsgroup(1, "*" . $search_param . "*", "subject", false, false, true);
			$page_list  = $result["pages"];
			$list_count = count ($result["articles"]);
			
			print "<!-- ";
			print_r ($result["exectime"]);
			print " --> ";
		
		 }
		 else if ($pp_sort=="")
		 { // list group by ARTICLE ID
		 
			$page_list  = $nntp->get_article_list($group_name);   
			$art_start  = $page_list[(count($page_list) - $start_at)-1];			                                           
			$result     = $nntp->get_message_summary( $art_start ); 
			$list_count = $gi["count"];
			
			print "<!-- ";
			print $start_at . " -- " . $list_count . " -- ";
			print $art_start; 
			print_r ($page_list);
			print "-->"; 
			                       
		 }
		 else if ($pp_sort=="alpha")
		 { // list group ALPHABETICALLY
			$letters ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			$letter  = $_GET["l"];
			if ($letter=="")
			{
			   $letter = "A";
			}
			$search_param = strtoupper($letter) . strtolower ($letter);
			
			raise_event (EVENT_PROCESS_WORKING, "Opening page '".$search_param."'."); 
			
			$result = $nntp->search_newsgroup(1, "[".$search_param."]*", "subject", false, false, true);
			
			raise_event (EVENT_PROCESS_WORKING, "Done. Processing..."); 
			$page_list  = $result["pages"];
			$list_count = count ($result["articles"]);  
			$page_count = count ($result["pages"]);  
			print "<!-- ";
			print_r ($result["exectime"]);
			print " --> ";
			
			print "<center>";
			for ($c=0;$c<strlen($letters);$c++)
			{
				$ltr = substr($letters,$c,1);
				if ($letter==$ltr)
				{
					print "<b>".$ltr."</b>&nbsp;&nbsp;&nbsp;";
				}
				else
				{
					print "<a href=\"javascript:fb_edit('s', '', 'l', '".$ltr."')\">".$ltr."</a>&nbsp;&nbsp;&nbsp;";
				} 
			}
			print "</center>";
			print "<hr noshade size=1>";
		 } 
		 #--------------------------------------------------------------------------------------
		 
		 
		$patterns=array();
		print "
		<form action='index.php' method='get' style='margin:0'>
		<font size=2 color='". $h1_text ."' face='Verdana, Arial, Helvetica, sans-serif'><b>" . $group_name ."</b></font>";
		
		if (array_search($group_name,$sg_c)===false)
		{
		    print "&nbsp;<a href=\"javascript:fb_edit ('a', 'subscribe')\">Subscribe</a>";
		}
		else
		{
		    print "&nbsp;<a href=\"javascript:fb_edit ('a', 'unsubscribe')\">Unsubscribe</a>";
		} 
		
		print "&nbsp;Sort by:";
		if ($pp_sort=="alpha")
		{
		    print "&nbsp;<a href=\"javascript:fb_edit ('s', '', 'pp_sort', '')\">Date</a>&nbsp;Title";
		}
		else
		{
		    print "&nbsp;Date&nbsp;<a href=\"javascript:fb_edit ('s', '', 'pp_sort', 'alpha')\">Title</a>";
		}
		
		print "
		
		<input type='text' size=6 name=fi value='".$search_param."'>
		
				   <input type='hidden' name='g' Value='".$group_name."'>
				   <input type='Submit' Value=' Go '>
				   </form>
		<br><br>"; 
		  
		 
		$first_page = floor($start_at / 10) * 10;                                                                                                          
		$last_page = $first_page + 10; 
		if ($last_page>count($page_list))
		{
			$last_page = count($page_list);
		}
		
		if ($last_page-$first_page>1)
		{
			
			if ($first_page > 0)
			{
				print "<a class='normal' href='javascript:fb_edit (\"s\", ". ($first_page-1) . ")'>&laquo; Previous ".(RESULT_PAGE_SIZE*10)."</a> | ";
			}
			else
			{
				# print "&laquo; Previous ".(RESULT_PAGE_SIZE*10)." | ";
			}
			if ($start_at > 0)
			{
				print "<a class='normal' href='javascript:fb_edit (\"s\", ". ($start_at-1) . ")'>&laquo;</a> | ";
			}
			
			for ($x=$first_page;$x<$last_page;$x++) 
			{ 
				if ( $start_at==$x ) 
				{ 
					print "<b>" . ($x+1) . "</b> | "; 
				} 
				else 
				{ 
					print "<a class='normal' href='javascript:fb_edit (\"s\", ".$x.",\"k\", ".$page_list[$x].")'>" . ($x+1) . "</a> | "; 
				}                                                                                                               
			} 
			
			if ($x<$last_page)
			{
				print "<a class='normal' href='javascript:fb_edit (\"s\", ". ($start_at+1) . ")'>&raquo;</a> | "; 
			}
			if ($x<count($page_list)-1)
			{
				print "<a class='normal' href='javascript:fb_edit (\"s\", ".$x.")'>Next ".(RESULT_PAGE_SIZE*10)." &raquo;</a>"; 
			}
			else
			{ 
				# print "Next ".(RESULT_PAGE_SIZE*10)." &raquo;";
			}
			
			$start_id = $page_list[(count($page_list) - $start_at)-1];
			$end_id   = $page_list[$start_at+1];
			
			print "  <small style='color:gray'>" . $start_id . "-" . $end_id . "</small>";
		}

		 
		$article_loop_count=0;		 
		$stopper     = min($start_id, $end_id);
		$nullpattern = array();
		foreach($result["articles"] as $article)
		{
		
			if ($article->nntp_message_id>=$stopper&&$article_loop_count<RESULT_PAGE_SIZE)
			{
				$article_loop_count++; 
				
				$pattern_is_present = false;
				while (list ($r,$g) = each ($patterns)) 
				{
					$pat_test = preg_match ($r, $article->subject, $tmp);
					if ($pat_test)
					{ 
					
				 
						$patterns[$r][count($patterns[$r])] = $article;
						$pattern_is_present = true;
						break;
					}
				}
				
				if ($pattern_is_present == false)
				{
				   #   $nullpattern[] = $article;
				}
				
				reset ($patterns);
				
				$pattern     = preg_quote($article->subject); #str_replace (".", "\\.", $article->subject);
				#$pattern     = str_replace ("+", "\\+", $pattern);
				#$pattern     = str_replace ("*", "\\*", $pattern);
				#$pattern     = str_replace ("=", "\\=", $pattern);
				#$pattern     = str_replace ("/", "\\/", $pattern);
				#$pattern     = str_replace ("[", ".+", $pattern);
				#$pattern     = str_replace ("]", ".+", $pattern);
				#$pattern     = str_replace ("(", ".+", $pattern);
				#$pattern     = str_replace (")", ".+", $pattern);
				
				$pattern     = preg_replace (array ("/(\d+)/", "/\//"), array("(\d+)", "\\/"), $pattern);  
				$pattern     = "/". trim($pattern) . "/i";
				
				if (array_key_exists($pattern, $patterns))
				{
				
				}		
				else
				{
				    $patterns[$pattern] = array($article);
				}  
			}
		} 
		
		
		
		 $html = "";
		 $preview = false;
		 $nonpics = array ();
		 $style = "border-bottom:solid 1px silver;border-top:solid 1px white";
		 $style = "";
		 
		 print "<script>articles = new Array();</script>";
		 
		 while (list ($a, $b) = each ($patterns)) 
		 {
			 $article = $b[0]; 
			 $tag     = $article->nntp_message_id;
		     $article->subject = htmlentities($article->subject);
			 
			 $image_test = preg_match("/\.(jpeg|jpg|gif|png)/i",$article->subject,$image_map); 
			 if ($preview==false)
			 {
				 $preview = $article;
			 }
			 if ($image_test) 
			 {
				 print "<script>
						articles.push(\"".$article->nntp_message_id."\");
						tmpgroup=new Array(\"".$article->nntp_message_id."\");
					   </script>";
			 }  
			
			 $html .= article_row($article, $group_name, $b);
			   
			 for ($bx=1;$bx<count($b);$bx++)
			 {
					$article=$b[$bx];
					$article->subject = str_replace("'", "&apos;", htmlentities($article->subject));
					if ($image_test) 
					{ 
						print "<script>
								tmpgroup.push(\"".$article->nntp_message_id."\"); 
								</script>";
					} 
					
					$html .=  article_row($article, $group_name, array(), $tag); 
			 }
			 
			 if ($image_test) 
			 { 
				 print "<script>
						artgroup['M".$tag."']=tmpgroup; 
					   </script>";
			 }  
		 }
		 
		 
		 while (list ($a, $b) = each ($nullpattern)) 
		 { 
			 $html .=  article_row($b, $group_name, array()); 
		 }
		 
		 
		 if ($preview)
		 {
		     $image_test = preg_match("/\.(jpeg|jpg|gif|png|bmp)/i",$preview->subject,$image_map); 
			 preview_table ($preview->nntp_message_id, $group_name, $image_test); 
		 }
		 else
		 {
		      preview_table ("X", $group_name); 
		 }
		 
		 ?>
		<!-- 
		
		<?= count ($patterns) ?>, 
		<?= count ($result["articles"]) ?>, 
		<?= count ($nullpattern) ?>, 
		<?= $article_loop_count ?>
		
		 message list -->
		 
		 
	 <table cellpadding="0" cellspacing="0" id="atr" cellspacing="0" style="border:solid 1px silver">
	 <tr>
		<td colspan=3>
		<a href="javascript:doTrans(true)"
		><font size=2 color="<?= $h1_text ?>" face="Verdana, Arial, Helvetica, sans-serif"><b>Articles</b></font></a> 
		<!--
		Images have been sorted into groups. <input type="button" id="bsample" value="Sample All" onClick="sample_msgs() ">
		-->
		</td>
	 </tr>
		  
		 <?
		 
		 print $html . "</table>";
		 
	}
	else
	{
	   print "Could not join " . $nntp_server . "/" . $group_name;
	   print $nntp->get_error_message();
	}
 }
 raise_event (EVENT_PROCESS_WORKING, ""); 
 $nntp->quit();
?>